/*
 ####        # 
 #           #
 ## ### #  # #
 #  #-# #\ # #
 #  # # # \# ####
 Author: Felipe de Andrade Neves Lavratti

 Copyright: There are no restrictions. Use as you want.	  
*/


#include "lpc_config.h"

#include "uart/uart.h"
#include "mprintf/mprintf.h"
#include "timer/timer.h"

#include "lcd/lcd_driver.h"
#include "lcd/pic/SELivre-LTF430-Logo.h"
#include "fdraw/fdraw.h"

#include "touchscreen/touchscreen.h"
#include "sdram/sdram.h"




/* Function prototypes */
void PLLInit( void );
void GPIOInit(void);
void App(void);

/* Main function only does the peripherical initialization and setup the pointer to the lcd pages. */
int main( void )
{
	int c;

	PLLInit();
	GPIOInit();

	UARTInit(115200,UART_CH_0,0);
	UARTTransmitString("\nselivre.wordpress.com\nDrawing Demo!\n", UART_CH_0);

	SDRAM_Init();

	LCD_Ctrl (FALSE);
	delayMs(100);
	LCD_Init (NULL, NULL, 0);
//	LCD_Change_Page (LogoPic.pPicStream, 0);
	LCD_Ctrl (TRUE);

	App();

	return 0;
}



/* This is the application function, it does only fill the lcd framebuffer page with the windows and
   monitor for touch at the touchpannel */
void App()
{
	int screen=1;
	char red=0, green=0, blue=0;
	int color=0;
	touchscreen_data ts_data;

	LCD_clear_page(1, 16, 0x00FFFFFF);
	LCD_draw_box(1,16, 0x00EEEEEE, 380, 479, 0, 30);
	LCD_draw_box(1,16, 0x00FF0000, 380, 479, 30, 60);
	LCD_draw_box(1,16, 0x0000FF00, 380, 479, 60, 90);
	LCD_draw_box(1,16, 0x000000FF, 380, 479, 90, 120);
	LCD_draw_box(1,16, 0x003366BB, 430, 480, 222, 272);
	LCD_draw_box(1,16, 0, 400, 460, 0, 30);
	LCD_draw_box(1,16, 0, 400, 460, 40, 50);
	LCD_draw_box(1,16, 0, 400, 460, 70, 80);
	LCD_draw_box(1,16, 0, 400, 460,100, 110);

	LCD_clear_page(0, 16, 0x000000FF);
	LCD_draw_dot(0,16,0xFF0000,0,0,0);
	LCD_draw_dot(0,16,0xFF0000,2,201,0);
	LCD_draw_dot(0,16,0xFF0000,54,201,0);
	LCD_draw_line(0,16,0xffffff,1,1,1,200, 0);
	LCD_draw_line(0,16,0xffffff,3,53,1,200, 0);

	LCD_draw_circle(0,16,0xffffff,300,120, 50, 0);
	LCD_draw_circle(0,16,0xffffff,300,120, 49, 0);
	LCD_draw_circle(0,16,0xffffff,300,120, 48, 0);
	LCD_draw_circle(0,16,0xffffff,300,120, 47, 0);
	LCD_draw_circle(0,16,0xffffff,300,120, 46, 0);
	LCD_draw_circle(0,16,0xffffff,300,120, 45, 0);
	LCD_draw_circle(0,16,0xffffff,300,120, 44, 0);
	LCD_draw_circle(0,16,0xffffff,300,120, 43, 0);

//	LCD_draw_rectangle(0,16,0xffffff,300,120, 50, 0);



	while (1)
	{
		switch(screen)
		{
		case 0:
			LCD_Change_Page(NULL,0);

			while(screen==0)
			{
				if(TSGetTouch(1, &ts_data))
				{
					if (ts_data.x >= 190 && ts_data.x <= 300)
						{
							if (ts_data.y >= 96 && ts_data.y <= 176) screen = 1;
						}
				}
			}
			break;

		case 1:
			LCD_Change_Page(NULL,1);
			while(screen==1)
			{
				if (TSGetTouch(1, &ts_data))
				{
					if (ts_data.x >= 380 && ts_data.x <= 480)
					{
						if (ts_data.y >= 222 && ts_data.y < 272 && ts_data.x >=430)
						{
							LCD_draw_box(1,16, 0x00FFFFFF, 0, 380, 0, 272);
							while(!TSGetDetouch());
						}
						else if (ts_data.y >= 30 && ts_data.y < 60)
						{
							if(ts_data.x < 400) red = 0;
							else if (ts_data.x > 460) red = 0xff;
							else red = (ts_data.x-400)*0xff/60;

							color = ((red<<16) | (green<<8) | blue);
							LCD_draw_box(1,16, color, 400, 460, 0, 30);
							LCD_draw_box(1,16, red<<16, 400, 460, 40, 50);
						}
						else if (ts_data.y >= 60 && ts_data.y < 90)
						{
							if(ts_data.x < 400) green = 0;
							else if (ts_data.x > 460) green = 0xff;
							else green = (ts_data.x-400)*0xff/60;

							color = ((red<<16) | (green<<8) | blue);
							LCD_draw_box(1,16, color, 400, 460, 00, 30);
							LCD_draw_box(1,16, green<<8, 400, 460, 70, 80);
						}
						else if (ts_data.y >= 90 && ts_data.y < 120)
						{

							if(ts_data.x < 400) blue = 0;
							else if (ts_data.x > 460) blue = 0xff;
							else blue = (ts_data.x-400)*0xff/60;

							color = ((red<<16) | (green<<8) | blue);
							LCD_draw_box(1,16, color, 400, 460, 00, 30);
							LCD_draw_box(1,16, blue, 400, 460,100, 110);
						}
					}
					else
					{
//						LCD_draw_line(1,16,color,ts_data.xlast, ts_data.x, ts_data.ylast, ts_data.y,0);
//						LCD_draw_box(1,16,color,ts_data.x-1, ts_data.x+1, ts_data.y-1, ts_data.y+1);
						LCD_draw_dot(1,16,color,ts_data.x,ts_data.y,0);

					}
					break;
				}
			}
			break;

		default:
			break;

		}
	}
}


void PLLInit( void )
{
	int i;
	#ifdef RUN_FROM_RAM
		/* Remap the interrupt vectors to RAM if we are are running from RAM. */
		SCB_MEMMAP = 2;
	#endif

	/* Disable the PLL. */
	PLLCON = 0;
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;

	/* Configure clock source. */
	SCS |= OSC_ENABLE;
	while( !( SCS & OSC_STAT ) );
	CLKSRCSEL = OSC_SELECT;

	/* Setup the PLL to multiply the XTAL input by 8. */
	PLLCFG = ( PLL_MUL | PLL_DIV );
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;

	/* Turn on and wait for the PLL to lock... */
	PLLCON = PLL_ENABLE;
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;
	/* Configurate CPU clock to divide PLL by 4... */
	CCLKCFG = CPU_CLK_DIV;
	while( !( PLLSTAT & PLL_LOCK ) );

	/* Connecting the clock. */
	PLLCON = PLL_CONNECT;
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;
	while( !( PLLSTAT & PLL_CONNECTED ) );

	MAMCR = 0;
	MAMTIM = 3;
	MAMCR = 2;

}
void GPIOInit(void)
{
	// Set to inputs
	IODIR0  = 0;
	IODIR1  = 0;
	FIO0DIR = 0;
	FIO1DIR = 0;
	FIO2DIR = 0;
	FIO3DIR = 0;
	FIO4DIR = 0;

	// Enable Fast GPIO0,1
	SCS|=0x01;
	// SCS_bit.GPIOM = 1;

	// clear mask registers
	FIO0MASK =0;
	FIO1MASK =0;
	FIO2MASK =0;
	FIO3MASK =0;
	FIO4MASK = 0;

	// Reset all GPIO pins to default primary function
	PINSEL0 =0;
	PINSEL1 =0;
	PINSEL2 =0;
	PINSEL3 =0;
	PINSEL4 =0;
	PINSEL5 =0;
	PINSEL6 =0;
	PINSEL7 =0;
	PINSEL8 =0;
	PINSEL9 =0;
	PINSEL10= 0;
	PINSEL11= 0;
}





